package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import com.leyou.utils.ResponseEntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(("spec"))
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    /**
     * 规格参数页,查询规格参数模版 组
     *
     * @param cid
     * @return
     */
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroups(@PathVariable("cid") Long cid) {
        List<SpecGroup> specGroupList = this.specificationService.querySpecGroups(cid);
        return ResponseEntityUtils.selectResultStatus(specGroupList);
    }


    /**
     * 规格参数页,查询规格参数模版 组内规格
     *
     * @param gid
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParam(
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "generic",required = false)Boolean generic,
            @RequestParam(value = "searching",required = false)Boolean searching) {
        List<SpecParam> specParamList = this.specificationService.querySpecParams(gid, cid,generic,searching);
        return ResponseEntityUtils.selectResultStatus(specParamList);
    }


    /**
     * 规格参数  新增分组
     *
     * @param specGroup
     * @return
     */
    @PostMapping("group")
    public ResponseEntity<Void> addGroup(@RequestBody SpecGroup specGroup) {

        boolean flag = this.specificationService.addGroup(specGroup);
        if (flag) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.ACCEPTED).build();
    }

    /**
     * 规格参数  修改分组信息
     *
     * @param specGroup
     * @return
     */
    @PutMapping("group")
    public ResponseEntity<Void> editGroup(@RequestBody SpecGroup specGroup) {
        try {
            this.specificationService.editGroup(specGroup);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.ACCEPTED).build();
        }
    }


    /**
     * 规格参数  删除分组信息
     * @param gid
     * @return
     */
    @DeleteMapping("group/{gid}")
    public ResponseEntity<Void> deleteGroup(@PathVariable("gid") Long gid) {
        try {
            this.specificationService.deleteGroup(gid);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.ACCEPTED).build();
        }
    }


    /**
     * 规格参数页 分组内的参数  新增参数
     * @param specParam
     * @return
     */
    @PostMapping("param")
    public ResponseEntity<Void> addArguments(@RequestBody SpecParam specParam) {
        try {
            this.specificationService.addArguments(specParam);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.ACCEPTED).build();
        }
    }


    /**
     * 规格参数页 分组内的参数  修改参数
     * @param specParam
     * @return
     */
    @PutMapping("param")
    public ResponseEntity<Void> editArguments(@RequestBody SpecParam specParam) {
        try {
            this.specificationService.editArguments(specParam);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.ACCEPTED).build();
        }
    }


    /**
     * 规格参数页 分组内的参数  删除参数
     *
     * @param pid
     * @return
     */
    @DeleteMapping("param/{pid}")
    public ResponseEntity<Void> deleteArguments(@PathVariable("pid") Long pid) {
        try {
            this.specificationService.deleteArguments(pid);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.ACCEPTED).build();
        }
    }
}
